/*我们将ASCII码值为0-31的非打印字符以八进制或者十六进制的数字字符输出，保留换行符不变*/
#include <stdio.h>

#define OCT 8
#define HEX 16
#define MAXLEN 10

int main(void)
{
	int n, ch, all;

	printf("请输入以什么进制打印非图形字符(o为八进制，h为十六进制): \n");
	
	ch = getchar();
	while(getchar() != '\n')
		continue;

	if(ch == 'h')
		n = HEX;
	else
		n = OCT;

	all = 0;
	while((ch = getchar()) != EOF)
	{
		if(ch >= 0 && ch <= 31 && ch != '\n')
		{
			if(all + 4 <= MAXLEN)
				all += 4;
			else
				putchar('\n');

			if(n == OCT)
				printf("0%-3o", ch);
			else
				printf("0x%-2x", ch);
		}
		else
		{
			putchar(ch);
			all++;
		}
		
		if(all % MAXLEN == 0)
		{
			putchar('\n');
			all = 0;
		}
	}
	return 0;
}
